/**
 * Created by yx on 2017/4/8.
 *
 */
function addHealthDiet() {

    var diet = {};
    diet.memberId = $("#memberId").val();
    diet.diteTimeStr = $("#health_diet_date").val();
    //diet.diteTime = $("#health_diet_date").val();
    diet.dietCount = $("#health_diet_cnt").val();
    diet.dietType = $("#health_diet_type").val();
    diet.dietUnit = $("#health_diet_unit").val();


    var coon =$("#diet_detail_list");
    var foods = [];
    coon.children(".row").each(
        function (index,ele) {
            var food = {};
            $(this).find("input,select").each(

                function (index2,ele2) {
                    if($(this).attr("name") =='foodType'){
                        food.foodType = $(this).val();
                    }
                    if($(this).attr("name") =='foodCount'){
                        food.foodCount = $(this).val()
                    }
                    if($(this).attr("name") =='foodUnit'){
                        food.foodUnit = $(this).val();
                    }
                }
            );
            foods.push(food);
        }
    );

    diet.dietDetail = foods;

    if(diet.memberId == ''){
        alert("客户ID不得为空");
        return false;
    }if(diet.dietUnit == ''){
        alert("单位不得为空");
        return false;
    }if(diet.dietType == ''){
        alert("类型不得为空");
        return false;
    }

    if(diet.dietCount!=''&&!checkNumf(diet.dietCount)){

        alert('数量必须为数字');
        return false;
    }

    if (!checkDatef(diet.diteTimeStr)){
        alert('时间格式错误');
        return false;
    }



    $.ajax({
        type: 'POST',
        url: "/member/addMemberHealthDietIndex",
        contentType:"application/json",
        data: JSON.stringify(diet),
        success: function (result) {
            alert("新增记录成功");
            $("#health-diet").click();
        },
        dataType: "text"
    });

}

function chengeMemberHealthDiet() {

    var memberId = $("#selectMemberId").val();


    $("#main_context").empty();
    $("#main_context").load("/member/healthDietIndex?memberId="+memberId);


}


function addFood() {



    var optionsvalue = '';

    $.get("/member/getAllFoodType",function(data,status){
        for(var gs in data.foodTypeList){
            optionsvalue +='<option value = "'+data.foodTypeList[gs].foodType+'" >'+ data.foodTypeList[gs].foodType+ '('+data.foodTypeList[gs].calorie+')'+'</option>';
        }

        $("#diet_detail_list").append(
            '<div class="row">'+
            '   <div class="col-lg-6 form-group">'+
            '       <label>食物种类</label>'+
            '       <select name="foodtype" class="form-control select2" style="width: 100%;">'+
            optionsvalue+
            // '           <option value="面包" >面包</option>'+
            // '           <option value="鸡蛋" >鸡蛋</option>'+
            // '           <option value="包子" >包子</option>'+
            // '           <option value="豆浆" >豆浆</option>'+
            // '           <option value="油条" >油条</option>'+
            // '           <option value="粥" >粥</option>'+
            '       </select>'+
            '   </div>'+
            '   <div class="col-lg-3 form-group">'+
            '       <label class="col-lg-12">摄入量</label>'+
            '       <input type="text" name="connTelephone" class="form-control" '+
            '              placeholder="点击输入..." >'+
            '   </div>'+
            '   <div class="col-lg-3 form-group">'+
            '       <label class="col-lg-12">摄入单位</label>'+
            '       <select id="health_diet_unit" class="form-control select2" style="width: 100%;">'+
            '           <option value="克" >克</option>'+
            '           <option value="升" >升</option>'+
            '       </select>'+
            '    </div>'+
            '</div>'
        );
    });

    // $.ajax({
    //     type: 'POST',
    //     url: "/member/getAllFoodType",
    //     contentType:"application/json",
    //     success: function (result) {
    //         // alert(result);
    //
    //     },
    //     dataType: "text"
    // });


// <#list food_type_list as food_type>
//     <option value='${food_type.foodType}'>${food_type.foodType}(${food_type.calorie})</option>
//         </#list>



}

function setDietDetail(item) {

    $("#diet-detail-context").empty();
    $("#diet-detail-context").load("/member/DietDetailIndex?id="+item);
}

function changeFoodType() {
    var v = $("#calorie").value;
    alert(v.split(",")[1]);
    var calorie = v.split(",")[1];
    $("#calorie").value=calorie;
}